package com.example.javaee_library.Controller;

import com.example.javaee_library.Entity.Book;
import com.example.javaee_library.Entity.User;
import com.example.javaee_library.Service.Book_Service;
import com.example.javaee_library.dto.AppointExecution;
import com.example.javaee_library.dto.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author Miao-yongzheng
 * @create 2021-12-10 16:09
 */
@Controller
@RequestMapping("/Customer/book")
public class BookController {
    @Autowired
    Book_Service book_service;


    @ResponseBody
    @RequestMapping(value = "/Appoint", method = RequestMethod.GET)
    private AppointExecution Appoint(
            Model model,
            @RequestParam("book_id")long bookId,
            HttpServletRequest request,
            HttpServletResponse response
                               ){
        User User = (User) request.getSession().getAttribute("user");
        int studentId = User.getId();
        System.out.println(bookId+" "+studentId+"调用借阅成功了");
        AppointExecution appointExecution = book_service.appoint(bookId,studentId);
        model.addAttribute("appointExecution",appointExecution);
        // list.jsp + model = ModelAndView
        return appointExecution;// WEB-INF/jsp/"list".jsp
    }

    @RequestMapping(value = "/Serach", method = RequestMethod.GET)
    private ModelAndView Serach(ModelAndView model,@RequestParam("bookName")String bookName,
                                HttpServletResponse response,
                                HttpServletRequest request) {
        List<Book> list = book_service.Serach(bookName);
        request.getSession().setAttribute("list",list);
        System.out.println(list.get(0).getName());
        model.addObject("list",list);
        // list.jsp + model = ModelAndView
        model.setViewName("myAppoint");
        return model;// WEB-INF/jsp/"list".jsp
    }


    @ResponseBody
    @RequestMapping(value = "/Appoint_again", method = RequestMethod.GET)
    private JsonData Appoint_again(
            Model model,
            @RequestParam("book_id")long bookId,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        User User = (User) request.getSession().getAttribute("user");
        int studentId = User.getId();
        Timestamp createTime = new Timestamp(new Date().getTime());
        System.out.println(bookId+" "+studentId+"调用续借成功了");
        int appointExecution = book_service.appoint_again(bookId,studentId,createTime);
        model.addAttribute("appointExecution",appointExecution);
        // list.jsp + model = ModelAndView
        return JsonData.buildSuccess("续借成功");// WEB-INF/jsp/"list".jsp
    }
}
